### EXTREMISM AND TERRORISM: REBEL GOALS AND TACTICS IN CIVIL WARS ###
### RENANAH MILES JOYCE AND PAGE FORTNA ###
### PERSPECTIVES ON POLITICS [2024] ###

## Replication for figures A1 and A2 in the online appendix

# --------------------------------------------------------------------------------
# Install and load packages
rm(list=ls())

ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

packages <- c("corrplot", "tidyr")
ipak(packages)

# --------------------------------------------------------------------------------
# set working directory

# Load data
df <- read.csv("ReplicationData_CorrelationMatrix.csv")

# --------------------------------------------------------------------------------
# Figure A1 - correlation matrix for key variables (conflicts over government control)

# Subset to conflicts over government control
a1 <- subset(df, govtInc==1, select=-c(secession, govtInc))

# Label columns
colnames(a1) <- c("terror fatalities", "aim: ideology", "aim: identity", "democracy", "rebel strength",
                   "popular support", "multiple groups", "conflict intensity", "ethnic conflict", "financing",
                   "population", "Cold War")

# Graph correlation matrix
a1 %>%
  drop_na() %>%
  cor() %>%
  corrplot(method = "ellipse", type = "upper", order = "original", tl.col = "black", 
           tl.srt = 45, tl.cex = 0.7, diag = FALSE, cl.offset = 0.2, cl.ratio = 0.25,
           cl.align.text = "l", cl.cex = 0.7)

# --------------------------------------------------------------------------------
# Figure A2 - correlation matrix for key variables (conflicts over territory)

# Subset to conflicts over territory
a2 <- subset(df, govtInc==0, select=-c(ideology, id_major, govtInc))

colnames(a2) <- c("terror fatalities", "secession", "democracy", "rebel strength",
                   "popular support", "multiple groups", "conflict intensity", "ethnic conflict", "financing",
                   "population", "Cold War")

# Graph correlation matrix
a2 %>%
  drop_na() %>%
  cor() %>%
  corrplot(method = "ellipse", type = "upper", order = "original", tl.col = "black", 
           tl.srt = 45, tl.cex = 0.7, diag = FALSE, cl.offset = 0.2, cl.ratio = 0.25,
           cl.align.text = "l", cl.cex = 0.7)
